package com.miaosha.ms_04.service;

import com.miaosha.ms_04.dao.OrderDao;
import com.miaosha.ms_04.domain.OrderInfo;
import com.miaosha.ms_04.exception.GlobalException;
import com.miaosha.ms_04.result.CodeMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description //TODO
 * @Author Administrator
 * @DATE 2020/9/19 16:02
 **/
@Service
public class OrderService {

    @Resource
    private OrderDao orderDao;

    public OrderInfo insert(OrderInfo orderInfo) {
        if (orderInfo == null) {
            throw new GlobalException(CodeMsg.fillParamEmptyArgs("订单信息"));
        }
         orderDao.insert(orderInfo);
        return orderInfo;
    }

    public OrderInfo getOrderInfoById(Long orderId) {
        if (orderId == null) {
            throw new GlobalException(CodeMsg.ORDER_ID_EMPTY);
        }
        return orderDao.getOrderInfoById(orderId);
    }

    public Boolean repeatOrderByUserIdAndGoodsId(Long userId, Long goodsId) {
        if (userId == null) {
            throw new GlobalException(CodeMsg.USER_EMPTY);
        }
        if (goodsId == null) {
            throw new GlobalException(CodeMsg.GOOD_ID_EMPTY);
        }
        return orderDao.countByUserIdAndGoodsId(userId, goodsId) < 1;
    }
}
